
package com.joysuch.wwyt.dataperm.processor;

import java.util.Arrays;

import com.joysuch.wwyt.core.sqlparser.impl.DataPermissionSqlParserInterceptor;
import com.joysuch.wwyt.dataperm.context.DataPermContext;
import com.joysuch.wwyt.dataperm.context.DataPermContextHolder;
import com.joysuch.wwyt.dataperm.context.DataPermRegisterInfoContext;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataPermissionProcessorBySqlParser {

    private static final Logger logger = LoggerFactory.getLogger("DataPermissionProcessorBySqlParser");
    private DataPermissionSqlParserInterceptor interceptor = new DataPermissionSqlParserInterceptor();

    public String process(String sql) throws JSQLParserException {
        // 未启用数据权限，直接返回
        if (!DataPermContextHolder.isDataPermEnabled()) {
            return sql;
        }
        if (sql.startsWith("{")) {
            return sql;
        }
        long start = System.currentTimeMillis();
        Statement parse = CCJSqlParserUtil.parse(sql);
        interceptor.intercept(parse);
        long end = System.currentTimeMillis();
        sql = parse.toString();
        long time = end - start;
        if (time > 50) {
            logger.debug(sql + " -> " + sql + " cost " + time + "ms");
        }
        return sql;
    }

    public static void main(String[] args) {
        String sql = "SELECT * FROM wwyt_api_mh_monitor_point_data AS a left join wwyt_api_test b on a.test_id = b.id WHERE a.tag_name IN (SELECT b.point_code FROM `wwyt_mh_platform_point` AS b WHERE "
                + "b.delete_flag ='0' AND b.switch_state = 1 AND b.platform_id =?1 AND a.tag_name = b.point_code) AND a.tag_time BETWEEN ?2 AND ?3 and b.name = ?4";
        String sql2 = "select t1 from table1 t1 union all select t2.b,t3.c from table2 t2,table3 t3 where t2.b = t3.c and t2.c in (select id from app)";
        DataPermContextHolder.setDataPermTopSwitch(true);
        DataPermContextHolder.setContext(createContext());

        DataPermRegisterInfoContext.addInfo("table1", "depart_id", "departId", null);
        DataPermRegisterInfoContext.addInfo("table2", "depart_id", "departId", null);
        DataPermRegisterInfoContext.addInfo("app", "depart_id", "departId", null);
        DataPermRegisterInfoContext.addInfo("wwyt_api_test", "depart_id", "departId", null);
        DataPermRegisterInfoContext.addInfo("wwyt_api_mh_monitor_point_data", "depart_id", "departId", null);
        try {
            String newSql = new DataPermissionProcessorBySqlParser().process(sql);
            System.out.println(newSql);
        } catch (JSQLParserException e) {
            e.printStackTrace();
        }
    }

    private static DataPermContext createContext() {
        DataPermContext c = new DataPermContext();
        c.setDeparts(Arrays.asList(1L, 3L, 5L, 7L, 999L));
        return c;
    }

}
